package Model;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.filechooser.*;
import Model.Trip;
import AirLine.Flight;
import AirLine.*;
import javax.swing.*;
import java.sql.*;


public class BookingPanel extends JPanel implements ActionListener{


	private JButton book;
        private JPanel tripPanel;
        private JPanel passengerPanel;
        private ArrayList<Person> persons;
        
        private JTextField[] pnames;
        private JTextField[] pnumbers;
        private JComboBox[] pseats;
        
        private ArrayList<Booking> bookings;
        
 
        private Trip trip;
        private int nrPassengers;
        
	public BookingPanel(Trip trip, int nrPassengers)
	{
            
            pnames=new JTextField[nrPassengers];
            pnumbers=new JTextField[nrPassengers];
            pseats=new JComboBox[nrPassengers];
            
            
            
            this.trip=trip;
            this.nrPassengers=nrPassengers;
            book=new JButton("Book");
            book.addActionListener(this);
            tripPanel=new JPanel();
            passengerPanel=new JPanel();
            passengerPanel.setSize(new Dimension(700, 200));
            createTables();
            createPassengers();
            showTables();
            createWindow();
            

        }
	
        
        private void createWindow()
        {
            JFrame window = new JFrame("Jaeee");
            //window.setPreferredSize(new Dimension(400, 700));
            //window.setResizable(false);
            //window.setLayout(new BorderLayout());
            window.setBounds(0, 0, 1024, 768);
            //window.setBackground(Color.BLUE);

            //window.setLayout(new BorderLayout());
            window.setContentPane(this);
            window.pack();
            
            window.setVisible(true);        // Display the window
            //panel.repaint();
           // window.repaint();  
        }
        
	public void createTables()
	{
            String[] columns={"Airline", "Departure Airport", "Destination Airport", "Departure Time", "Destination Time", "Free Seats", "Price"};
                    
                   // DefaultTableModel model=new DefaultTableModel(7, t.getFlights().size()+1);       
                    JTable table=new JTable(trip.getFlights().size()+1, 7);
                    
                    
                    for(int k=0; k<columns.length; k++)
                    {
                        table.setValueAt(columns[k], 0, k);
                    }

                    int i=0;
                    for(Flight f: trip.getFlights())
                    {
                        i++;
                        table.setValueAt(f.getAirline().getName(), i, 0);
                        table.setValueAt(f.getDepartureAirport(), i, 1);
                        table.setValueAt(f.getDestinationAirport(), i, 2);
                        table.setValueAt(f.getDepartureTime(), i, 3);
                        table.setValueAt(f.getArrivalTime(), i, 4);
                        table.setValueAt(f.getFreeSeats().size(), i, 5);
                        table.setValueAt(f.getPrice(), i, 6);
                        
                    }
                  // tableList.add(table);
                   JTextField field=new JTextField(trip.getDepartureAirport()+"->"+trip.getDestinationAirport()+"  Duration: "+new Time(System.currentTimeMillis()+trip.getDuration()));
                   tripPanel.add(table);
                   tripPanel.add(field);
                   //buttonList.add(button);
       
	}
	
        private void createPassengers()
        {
                passengerPanel.setLayout(new GridBagLayout());
                GridBagConstraints c = new GridBagConstraints();
                c.fill = GridBagConstraints.HORIZONTAL;
                c.gridx=0;
                c.gridy=0;
            
            String[] seatList={"Window", "Isle", "Row"};
            for(int i=0; i<nrPassengers; i++)
            {
                c.gridx=0;
                //JPanel panel=new JPanel();
                JTextField pname=new JTextField(20);
                JTextField pnumber=new JTextField(12);
                
                
                JComboBox box=new JComboBox(seatList);
                
                passengerPanel.add(new JLabel("Passenger name: "),c);
                c.gridx++;
                passengerPanel.add(pname, c);
                c.gridx++;
                passengerPanel.add(new JLabel("SSN: "), c);
                c.gridx++;
                passengerPanel.add(pnumber, c);
                c.gridx++;
                c.gridx++;
                passengerPanel.add(box, c);
                c.gridx++;
                c.gridy++;
                
                pnames[i]=pname;
                pnumbers[i]=pnumber;
                pseats[i]=box;
//              .gridx++;

                //passengerPanel.add(panel);
            }
        }
        
        public void showTables()
        {
            
                this.setLayout(new GridBagLayout());
                GridBagConstraints c = new GridBagConstraints();
                c.fill = GridBagConstraints.HORIZONTAL;
                c.gridx=0;
                c.gridy=0;
            
            this.add(tripPanel, c);
            c.gridy++;
            c.gridy++;
            this.add(passengerPanel, c);
            c.gridy++;
            this.add(book, c);
        }
        
        public ArrayList<Booking> getBookings()
        {
            return bookings;
        }
        
	public void actionPerformed(ActionEvent e) 
	{
            
            persons=new ArrayList<Person>();
            bookings=new ArrayList<Booking>();
            for(int i=0; i<nrPassengers; i++)
            {
                Person p=new Person(pnames[i].getText(), pnumbers[i].getText());
                
                
                persons.add(p);
                
                if(p.getName().length()>0 && p.getSSN().length()>0)
                {
                for(Flight f: trip.getFlights())
                {
                    Set<Seat> seats=f.getFreeSeats();
                    
                    Seat seat=null;
                    for(Seat s: seats)
                    {

                        if(!s.isBooked() && s.getType()==pseats[i].getSelectedIndex())
                        {

                            seat=s;
                            
                        }
                        if(seat!=null)                       
                            break;
                        
                    }
                    
                    if(seat!=null)
                    bookings.add(new Booking(p, f, seat));
                    else
                        System.out.println("No free seat for that type, booking not done");
                }
                }
                else
                    System.out.println("One or more passengers missed info, rest has been booked");
            }
            
            for(Booking b: bookings)
            {
                System.out.println(b.getPerson().getName()+" har bokat: "+b.getFlight()+" med säte: "+b.getSeat().getType());
            }
            
//            System.out.println("mmm!!! "+e.getActionCommand());
//            String tripid=e.getActionCommand();
//            Trip t=buttonMap.get(tripid);
//            System.out.println("jaee"+t.getFlights());

            
            
	}
	
	public void windowActivated(WindowEvent e){}
     
	public void 	windowClosed(WindowEvent e){}
     
	public void 	windowClosing(WindowEvent e){
		
		System.exit(0);
	}
     
	public void 	windowDeactivated(WindowEvent e){}
     
	public void 	windowDeiconified(WindowEvent e){}
    
	public void 	windowIconified(WindowEvent e){}
    
	public void 	windowOpened(WindowEvent e){}
    
	
}
